function Population = initializePopulation(M, params,DSM)
    % 初始化具有异构染色体大小的种群
    Population = cell(params.PopulationSize, 1);
    popCount = 0;

    % 1. 确定不同数量模块的目标数量
    meanModules = sqrt(M) + 2;% M是元素数量，取得根号M的值，
    % 这是基于文献中一种启发式设定：如果你有 M 个组件，那么用 √M 个模块是合理的初始估计
    stdDev = 2; % 设置标准差为 1.0。
    min_rows = max(2, floor(meanModules - 2 * stdDev)); % 计算模块数量的下限，floor是向下取整
    max_rows = min(M, ceil(meanModules + 2 * stdDev)); % 计算模块数量的上限，ceil是向上取整

    moduleProbs = zeros(max_rows, 1); % 创建一个列向量 moduleProbs，存储每个候选模块数量的概率，初始值全为 0
    rowNumbers = min_rows:max_rows; % 创建一个向量 rowNumbers，列出所有候选模块数量（从最小模块数到最大模块数）
    
    % 根据正态分布给候选模块数量分配概率
    moduleProbs(rowNumbers) = normpdf(rowNumbers, meanModules, stdDev);
    
    totalProb = sum(moduleProbs);
    if totalProb <= 0
        warning('Probability calculation failed, defaulting module count.');
        moduleCounts = containers.Map('KeyType','double','ValueType','double');
        moduleCounts(max(2,round(meanModules))) = params.PopulationSize;
    else
        normalizedProbs = moduleProbs / totalProb; %把之前的概率密度归一化为真正的概率分布
        % normalizedProbs(i) 表示第 i 个模块数（rowNumber 为 i）所占种群比例
        targetCounts = round(normalizedProbs * params.PopulationSize); %计算每种模块数对应的个体数量，并进行四舍五入
        
        % 调整计数以完全匹配 PopulationSize
        currentTotal = sum(targetCounts);
        diff = params.PopulationSize - currentTotal; %计算四舍五入后与目标种群数量的差值 diff
        if diff ~= 0
            [~, sortedProbIdx] = sort(normalizedProbs, 'descend'); %根据概率值从高到低对模块数量索引进行排序，
            % sortedProbIdx 是按概率降序排列的模块数量下标，优先调整高概率的模块数
            adjustIndices = sortedProbIdx(1:abs(diff)); % 获取要调整的模块数量索引前 |diff| 个
             % 调整前确保指数有效
             validAdjustIndices = adjustIndices(adjustIndices > 0 & adjustIndices <= max_rows);
             %从 adjustIndices 向量中筛选出满足特定条件的元素，并将结果存储在 validAdjustIndices
             if ~isempty(validAdjustIndices)
                 targetCounts(validAdjustIndices) = targetCounts(validAdjustIndices) + sign(diff);
             else % 如果索引无效则回退
                 targetCounts(max(2,round(meanModules))) = targetCounts(max(2,round(meanModules))) + diff;
             end
             % 重新计算总数以确保安全，尽管现在应该匹配
             currentTotal = sum(targetCounts);
             if currentTotal ~= params.PopulationSize
                 % 最终后备调整
                 targetCounts(max(2,round(meanModules))) = targetCounts(max(2,round(meanModules))) + (params.PopulationSize - currentTotal);
             end
        end
        
        moduleCounts = containers.Map('KeyType','double','ValueType','double');
        validRows = find(targetCounts > 0);
        %找出所有分配了个体的模块数索引；
        %targetCounts 是一个下标从 min_rows 到 max_rows 的向量；
        %例如 targetCounts(5) = 12 就意味着模块数为 5 时分配了 12 个个体
        for r = validRows' % 迭代有效行
             moduleCounts(r) = targetCounts(r);
        end
    end


    fprintf('Target module counts distribution: ');
    keys = cell2mat(moduleCounts.keys);
    vals = cell2mat(moduleCounts.values);
    for k_idx = 1:length(keys)
        fprintf('%d:%d ', keys(k_idx), vals(k_idx));
    end
    fprintf('\n');
    
    % 2. Generate individuals
    moduleNums = cell2mat(moduleCounts.keys);
    counts = cell2mat(moduleCounts.values);

    for i = 1:length(moduleNums)
        num_modules = moduleNums(i);
        count = counts(i);
        for j = 1:count
            if popCount >= params.PopulationSize
                break;
            end
            % 为简单起见，使用“随机”生成。完整实现请替换为“greedy_roulette”。M, num_modules_target, method, DSM, greedyThreshold
            chromosome = generateSingleChromosome(M, num_modules, 'greedy_roulette', DSM, 0.5);
            if ~isempty(chromosome)
                popCount = popCount + 1;
                Population{popCount} = chromosome;
            end
        end
         if popCount >= params.PopulationSize; break; end
    end

    % 如果需要，填补剩余的位置（例如，生成失败）
    while popCount < params.PopulationSize
        num_modules = max(2, round(sqrt(M))); % Default to mean
        chromosome = generateSingleChromosome(M, num_modules, 'greedy_roulette');
        if ~isempty(chromosome)
             popCount = popCount + 1;
             Population{popCount} = chromosome;
        end
    end
    
    % 如果有多余的，则修剪掉（按照目前的逻辑不应该发生这种情况，但是为了安全）
    Population = Population(1:params.PopulationSize); 
end
